<?php
/**
 *
 * User: wufeng
 * Date: 2020/03/07
 */

namespace app\models;
use think\Model;

class MyNoticeObjectModel extends Model
{
	protected $table = 'edu_my_notice_object';


	/**
	 * User: wufeng
	 * Date: 2020/03/24
	 *
	 * @param $params
	 * @return array
	 * @throws \think\exception\DbException
	 */
	public function getMyNoticeObjectLists($params)
	{
		$page = isset($params['page']) ? (int) $params['page'] : 1;
		$listRows = isset($params['limit']) ? (int) $params['limit'] : 10;
		$where = [];
		if (isset($params['day']) && '' !== $params['day']) {
			$date = explode(' - ', $params['day']);

			$start = date('Y-m-d H:i:s', strtotime($date[0]));
			$end = date('Y-m-d H:i:s', strtotime($date[1]) + 86400);
			$where['create_time'] = [['egt', $start], ['lt', $end]];
		}

		$where['notice_object_id'] = get_current_id();
		$listData = self::where($where)
			->order('id desc,is_read desc')
			->paginate($listRows, false, ['page' => $page])
			->toArray();
		if (!empty($listData)) {
			foreach ($listData['data'] as &$item) {
				$myNotice = (new MyNoticeModel())->get($item['notice_id']);
				$item['notice_name'] = $myNotice['notice_name'];
				$item['notice_time'] = $myNotice['notice_time'];
				$item['user_id'] = $myNotice['user_id'];
				$item['user_name'] = (new UserModel())->get($myNotice['user_id'])['name'];
				$item['content'] = $myNotice['content'];
				$item['audio'] = $myNotice['audio'];
				$item['audio_duration'] = $myNotice['audio_duration'];
				$item['video'] = $myNotice['video'];
				$item['picture'] = explode(',', $myNotice['picture']) ? explode(',', $myNotice['picture']) : [];
				$item['s_notice_type'] = $item['notice_type'];
				$item['status'] = $myNotice['status'];
				if(1 === (int) $item['is_read']){
					$item['is_read_text'] = '<span style="color:#FF5722;font-weight: bold;">已读</span>';
				}elseif (0 === (int) $item['is_read']){
					$item['is_read_text'] = '<span style="color:#01AAED;font-weight: bold;">未读</span>';
				}
			}
		}

		return $listData;
	}
}